<?php
class Order_Model_OrderShip
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Order_Model_DbTable_OrderShip();
	}
	
	public function getAdapter(){
		return $this->_table->getAdapter(); 
	}
	/**
	*Create A Record  
	*@param rowSet 
	*@return boolean
	*/
	public  function create($row){
 		return $this->_table->insert($row); 
	}

	/**
	*Update One Row  
	*@param rowSet $row 
	*@param int $order_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$where = $this->_table->getAdapter()->quoteInto("os_id= ?", $id);
		return $this->_table->update($row, $where);
	}
	public  function deleteByOrderCode($orderCode){
		$where = $this->_table->getAdapter()->quoteInto("orders_code= ?", $orderCode);
		return $this->_table->delete( $where);
	}
	
	public  function updateByOrdersCode($row,$orders_code){
		$where = $this->_table->getAdapter()->quoteInto("orders_code= ?", $orders_code);
		return $this->_table->update($row, $where);
	}
	
	
	public  function updateByOrdersCodeAndOsid($row,$orders_code,$os_id){
		$where = $this->_table->getAdapter()->quoteInto("orders_code= ?", $orders_code);
		$where .= $this->_table->getAdapter()->quoteInto(" and os_id != ?", $os_id);
        $row['os_last_update']=date('Y-m-d H:i:s');
		return $this->_table->update($row, $where);
	}
	
	public function getByOrdersCode($ordersCode,$orderBy=array('os_id DESC'),$type='*'){
	
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table,$type);
		$select->where(" orders_code = ?",$ordersCode);
		$select->order($orderBy);
		$sql = $select->__toString();
		return $this->_table->getAdapter()->fetchRow($sql); 
	}
	
	public function getByTrackingNumber($tracking_number){		
		$where = $this->_table->getAdapter()->quoteInto("tracking_number= ?", $tracking_number);
		$where.= $this->_table->getAdapter()->quoteInto(" or ref_tracking_number= ?", $tracking_number);
// 		echo $where;exit;
		$result =  $this->_table->fetchAll($where);
		if($result instanceof Zend_Db_Table_RowSet){
			return $result->toArray();
		}
		return $result;		
		
	}
	
	
	public function getByfield($field=1,$value=0){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchAll($where);
	}
	
	
	public function getLastRowByOrdersCode($orders_code){
		$where = $this->_table->getAdapter()->quoteInto("select * from order_ship where os_status>0 and orders_code = ? order by os_id desc limit 1",$orders_code);
		return $this->_table->getAdapter()->fetchRow($where);
	}
	
	
    public function getByCondition($condition=array(),$type='*',$orderby=array('os_id DESC'),$pageSize=0,$page=1){
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table,$type);
		$select->where("1 =?",1);
		if(isset($condition["orders_code"]) && $condition["orders_code"]!=''){
			$select->where(" orders_code = ?",$condition["orders_code"]);
		}
		if(isset($condition["os_status"]) && $condition["os_status"]!=''){
			$select->where(" os_status = ?",$condition["os_status"]);
		}
        if (isset($condition["tracking_number"]) && $condition["tracking_number"] != '') {
            $select->where(" tracking_number = ?", $condition["tracking_number"]);
        }
        if (isset($condition["ref_tracking_number"]) && $condition["ref_tracking_number"] != '') {
            $select->where(" ref_tracking_number = ?", $condition["ref_tracking_number"]);
        }
		if('count(*)'==$type){
			return $this->_table->getAdapter()->fetchOne($select);
		}else{
			$select->order($orderby);
			if($pageSize>0 and $page>0)
			{
				$start =($page-1)*$pageSize;
				$select->limit($pageSize,$start);
			}
			$sql=$select->__toString();
			return $this->_table->getAdapter()->fetchAll($sql);
		}
	}

    public function getByValue($value,$field='ref_tracking_number'){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("os_status > 0");
        $select->where("{$field} = ?", $value);
        $select->order(array('os_id desc'));
        return $this->_table->getAdapter()->fetchRow($select);
    }

	
}